/*
 * @Author: jiejie
 * @Github: https://github.com/jiejieTop
 * @Date: 2019-07-15 23:56:23
 * @LastEditTime: 2019-12-05 23:05:36
 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
 */
#ifndef __MEM_H_
#define __MEM_H_

#ifndef D_NULL
#define D_NULL    ((void *)0)
#endif

typedef unsigned int d_uint32_t; 
typedef unsigned char d_uint8_t; 
typedef char d_err; 

typedef enum {
    D_FALSE = 0x00,
    D_TRUE,
}d_bool;

typedef enum mem_error {
    D_OK = 0x00,
    D_NOK
}d_err_t;

/* Memory node information control block */
typedef struct doge_mem_heap_node {
    struct doge_mem_heap_node* prev;
    d_uint32_t mem_node_size : 30 ;  /* mem node size */
    d_uint32_t mem_used : 1;         /* used or no */
    d_uint32_t mem_align : 1;        /* align or no, reserved */
    d_uint8_t  user_mem[];
} dog_mem_heap_node_t;

/* Memory heap management information control block */
typedef struct doge_mem_heap_info {
    void *mem_heap_addr;         /* Starting address of memory heap */
    d_uint32_t mem_heap_size;        /* Memory Heap size */
    struct doge_mem_heap_node *MemHead;
    struct doge_mem_heap_node *mem_tail;
} dog_mem_heap_info_t;


#define   MEM_INFO_SIZE   sizeof(struct doge_mem_heap_info)
#define   MEM_NODE_SIZE   sizeof(struct doge_mem_heap_node)

/* Memory alignment */
#define DOG_ALIGN(addr, size)           (((addr) + (size - 1)) & ~((size) - 1))

#define DOG_ALIGN_DOWN(addr, size)      ((addr) & ~((size) - 1))

d_bool dog_mem_heap_init(d_uint8_t *mem_start_addr, d_uint32_t mem_size, void(*lock)(void),  void(*unlock)(void));
void* dog_mem_alloc(d_uint32_t size);
d_err_t dog_mem_free(void *mem);
d_uint32_t dog_get_mem_heap_info(void);

#endif /* __REDEF_H */


/********************************END OF FILE***************************************/

